package com.hutiao.snowhb.service;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hutiao.snowhb.common.RandomId;
import com.hutiao.snowhb.common.WeixinUtil;
import com.hutiao.snowhb.dao.BaseDao;
import com.hutiao.snowhb.entities.WeixinUserInfo;
import com.hutiao.snowhb.resource.ConfigInfo;

@Component
public class UserService {

	@Autowired
	private BaseDao baseDao;
	
	
	public void saveUser(WeixinUserInfo wxu) {
		
		RandomId r = new RandomId();
		int uid = Integer.parseInt(r.randomId(new java.util.Random().nextInt(100)));
		
		String sql = "insert into snow_user(uid,subscribe,openId,nickname,sex,"
				+ "city,country,province,"
				+ "language,headimgurl,subscribeTime,createTime,updateTime) "
				+ "values("+uid+","+wxu.getSubscribe()+",'"+wxu.getOpenId()+"','"+wxu.getNickname()+"',"+wxu.getSex()+","
				+ "'"+wxu.getCity()+"','"+wxu.getCountry()+"','"+wxu.getProvince()+"',"
				+ "'"+wxu.getLanguage()+"','"+wxu.getHeadimgurl()+"',FROM_UNIXTIME("+wxu.getSubscribe_time()+"),NOW(),NOW()) "
				+ "ON DUPLICATE KEY UPDATE subscribe="+wxu.getSubscribe()+",nickname='"+wxu.getNickname()+"',sex='"+wxu.getSex()+"', "
				+ "city='"+wxu.getCity()+"',country='"+wxu.getCountry()+"',province='"+wxu.getProvince()+"', "
				+ "language='"+wxu.getLanguage()+"',headimgurl='"+wxu.getHeadimgurl()+"',subscribeTime=FROM_UNIXTIME("+wxu.getSubscribe_time()+"),updateTime=NOW();";
		
		this.baseDao.execute(sql);
		
		return;
	}
	
	public void unsubscribe(String openId) {
		
		String sql = "update snow_user set subscribe=0 where openId='"+openId+"'";
		this.baseDao.execute(sql);
		
		return;
		
	}
	
	
	public WeixinUserInfo getWxUserInfo(String openId) throws Exception {
		
		WeixinUserInfo user = null;
		
		String sql = "select uid,subscribe,openId,nickname,sex,city,country,province, "
				+ "language,headimgurl,UNIX_TIMESTAMP(subscribeTime) as subscribeTime "
				+ "from snow_user where openId='"+openId+"' ";
		
		this.baseDao.queryForMap(sql);
		
		Map<String, Object> temp = this.baseDao.queryForMap(sql);
		
		if (temp == null) {
			user = WeixinUtil.getUserInfo(openId);
			
			if (user.getSubscribe() == 0) {
				return user;
			}
			
			this.saveUser(user);
			return user;
		}
		
		user = new WeixinUserInfo();
		user.setSubscribe(Integer.parseInt(temp.get("subscribe").toString()));
		user.setOpenId(temp.get("openId").toString());
		user.setNickname(temp.get("nickname").toString());
		user.setSex(Integer.parseInt(temp.get("sex").toString()));
		user.setCity(temp.get("city").toString());
		user.setCountry(temp.get("country").toString());
		user.setProvince(temp.get("province").toString());
		user.setLanguage(temp.get("language").toString());
		user.setHeadimgurl(temp.get("headimgurl").toString());
		user.setSubscribe_time(Long.parseLong(temp.get("subscribeTime").toString()));
		
		return user;
	}
	
	public WeixinUserInfo defaultWxUser() {
		
		WeixinUserInfo user = new WeixinUserInfo();
		user.setHeadimgurl(ConfigInfo.wxHeadImg);
		user.setHeadimgurl64(ConfigInfo.wxHeadImg64);
		user.setNickname("anonym");
		
		return user;
	}
	
	public WeixinUserInfo virtualMM() {
		WeixinUserInfo user = new WeixinUserInfo();
		user.setHeadimgurl(ConfigInfo.wxHeadImg);
		user.setHeadimgurl64(ConfigInfo.wxHeadImg64);
		user.setNickname("雪花妹");
		
		return user;
	}
}
